#include <cstdio>

const int N = 100;
int n, m;
bool flag = false;

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("testdata.in", "r", stdin);
  freopen("testdata.out", "w", stdout);
#endif
#ifndef LOCAL
  freopen("T2.in", "r", stdin);
  freopen("T2.out", "w", stdout);
#endif
#endif

  scanf("%d%d", &n, &m);
  for (int i = 1, u, v; i < n; ++i) {
    scanf("%d%d", &u, &v);
  }
  for (int i = 1, a, b, d; i <= m; ++i) {
    scanf("%d%d%d", &a, &b, &d);
    if (d != 2e6) flag = true;
  }
  if (flag)
    puts("NO");
  else
    puts("1");
  return 0;
}